package com.itcam.cm.cart.service.impl;

import com.itcam.cm.cart.service.CartService;
import com.itcam.cm.model.entity.cart.CartInfo;
import com.itcam.cm.model.entity.product.ProductSku;
import com.itcam.cm.model.entity.user.AuthContextUtil;
import com.itcam.cm.model.entity.user.UserInfo;
import com.itcam.cm.feign.product.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    public void addToCart(Long skuId, Integer skuNum) {
        UserInfo user = AuthContextUtil.getUser();
        Long userId = user.getId();
        // 购物车之前是否添加过 定义key user:cart:userId
        Boolean isHas = redisTemplate.opsForHash().hasKey("user:cart:" + userId, skuId+"");
        if(isHas){
            // 修改
            CartInfo cartInfo = (CartInfo)redisTemplate.opsForHash().get("user:cart:" + userId,skuId+"");
            cartInfo.setSkuNum(cartInfo.getSkuNum()+skuNum);
            redisTemplate.opsForHash().put("user:cart:" + userId,skuId+"",cartInfo);
        }else {
            // 添加
            // 根据skuId查询商品信息
            ProductSku productSku = productFeignClient.getSkuById(skuId);
            CartInfo cartInfo = new CartInfo();
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setSkuNum(skuNum);
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuName(productSku.getSkuName());
            cartInfo.setSkuId(productSku.getId());
            cartInfo.setIsChecked(1);
            cartInfo.setUserId(userId);
            redisTemplate.opsForHash().put("user:cart:" + userId,skuId+"",cartInfo);
        }

    }

    @Override
    public List<CartInfo> cartList() {
        UserInfo user = AuthContextUtil.getUser();
        Long userId = user.getId();
        List<CartInfo> cartInfos = (List<CartInfo>)redisTemplate.opsForHash().values("user:cart:" + userId);
        return cartInfos;
    }

    @Override
    public void deleteCart(Long skuId) {
        UserInfo user = AuthContextUtil.getUser();
        Long userId = user.getId();
        redisTemplate.opsForHash().delete("user:cart:" + userId,skuId+"");
    }

    @Override
    public void checkCart(Long skuId, Integer isChecked) {
        UserInfo user = AuthContextUtil.getUser();
        Long userId = user.getId();
        CartInfo cartInfo = (CartInfo)redisTemplate.opsForHash().get("user:cart:" + userId,skuId+"");
        cartInfo.setIsChecked(isChecked);
        redisTemplate.opsForHash().put("user:cart:" + userId,skuId+"",cartInfo);
    }

    @Override
    public List<CartInfo> getAllChecked() {

        // 用户id
        UserInfo user = AuthContextUtil.getUser();
        Long userId = user.getId();
        String cartKey = "user:cart:" + userId;
        List<CartInfo> cartInfos = (List<CartInfo>)redisTemplate.opsForHash().values(cartKey);

        List<CartInfo> cartList = cartInfos.stream().filter(cartInfo -> cartInfo.getIsChecked() == 1).collect(Collectors.toList());

        // 获取所有的购物项数据
        return cartList;
    }

    @Override
    public void deleteCheckedCart() {
        System.out.println("删除已经提交订单的购物车数据");
    }
}
